﻿#Basic effect for gaining a court position
grand_consort_gained_court_position_effect = {
	scope:liege = {
		save_scope_as = actor
		primary_spouse ?= {
			save_scope_as = liege_actor_primary_spouse
		}
		scope:employee = {
			save_scope_as = grand_consort
		}
		hidden_effect = {
			# Shared effects
			every_courtier = {
				limit = {
					NOR = {
						this = scope:employee
					}
					is_valid_harem_member_trigger = {
						LIEGE = scope:liege
					}
				}
				add_to_list = harem_members_list
			}
			every_in_list = {
				list = harem_members_list
				send_interface_toast = {
					type = event_generic_bad
					title = grand_consort_promotion_toast
					right_icon = scope:grand_consort
					left_icon = scope:liege
					custom_tooltip = grand_consort_promotion_toast_desc
					
					if = {
						limit = {
							scope:grand_consort = {
								NOT = {
									has_demoted_or_divorced_as_ps_opinion_trigger = {
										TARGET = scope:liege
									}
								}
							}
						}
						#Update the primary spouse's opinion on the Grand Consort
						if = {
							limit = {
								is_head_of_the_harem_trigger = {
									LIEGE = scope:liege
								}
								OR = {
									has_trait = craven
									has_trait = paranoid
									has_trait = shy

									harem_politics_evil_consort_trigger = yes

									has_relation_rival = scope:employee
									has_relation_nemesis = scope:employee
								}
								NOR = {
									has_relation_best_friend = scope:employee
									has_relation_friend = scope:employee
									has_relation_soulmate = scope:employee
									has_relation_lover = scope:employee

									has_relation_soulmate = scope:liege

									has_trait = trusting
								}
								opinion = {
									target = scope:employee
									value <= low_positive_opinion
								}
							}
							add_opinion = {
								target = scope:employee
								modifier = threatening_consort_opinion
							}
							add_opinion = { #Feeling the pressure
								modifier = respect_opinion
								opinion = -10
								target = scope:liege
							}
						}
						#Consorts will update their opinions on new Grand Consort
						if = {
							limit = {
								harem_politics_evil_consort_trigger = yes
								trigger_if = {
									limit = {
										hp_accepts_harems = no
									}
									OR = {
										has_trait = ambitious
										has_trait = arrogant
										has_trait = greedy
									}
								}
								trigger_else = {
									hp_accepts_harems = yes
								}
								NOR = {
									is_head_of_the_harem_trigger = {
										LIEGE = scope:liege
									}

									has_relation_best_friend = scope:employee
									has_relation_friend = scope:employee
									has_relation_soulmate = scope:employee
									has_relation_lover = scope:employee

									has_relation_soulmate = scope:liege
								}
								opinion = {
									target = scope:employee
									value <= low_positive_opinion
								}
							}
							if = {
								limit = {
									is_consort_of = scope:liege
									NOR = {
										has_opinion_modifier = {
											target = scope:employee
											modifier = envy_consort_opinion
										}
										has_demoted_or_divorced_as_ps_opinion_trigger = {
											TARGET = scope:liege
										}
									}
								}
								add_opinion = {
									target = scope:employee
									modifier = envy_consort_opinion
								}
							}
							if = { # Your other relatives don't like you surpassing their authority
								limit = {
									NOT = {
										is_consort_of = scope:liege
									}
								}
								add_opinion = {
									modifier = respect_opinion
									opinion = -10
									target = scope:liege
								}
							}
						}
						if = {
							limit = {
								scope:employee = {
									harem_politics_evil_consort_trigger = yes
								}
								is_consort_of = scope:liege
							}
							stress_impact = {
								craven = minor_stress_impact_gain
								shy = minor_stress_impact_gain
								paranoid = miniscule_stress_impact_gain
							}
						}
					}
				}
			}
		}

		# Shared effects
		if = {
			limit = { 
				has_trait = just
				has_imprisonment_reason = scope:employee
			}
			stress_impact = {
				just = minor_stress_impact_gain
			}
		}
	}
	scope:employee = {
		# Shared effects
		if = {
			limit = {
				NOT = {
					has_demoted_or_divorced_as_ps_opinion_trigger = {
						TARGET = scope:liege
					}
				}
			}
			clear_negative_consort_modifiers_effect = yes
			if = {
				limit = {
					NOR = {
						has_opinion_modifier = {
							target = scope:liege
							modifier = forced_me_concubine_marriage_opinion
						}
						has_demoted_or_divorced_opinion_trigger = {
							TARGET = scope:liege
						}
					}
				}
				if = {
					limit = {
						trigger_if = {
							limit = {
								hp_accepts_harems = no
							}
							OR = {
								has_trait = ambitious
								has_trait = arrogant
								has_trait = greedy
							}
						}
						trigger_else = {
							hp_accepts_harems = yes
						}
					}
					add_opinion = {
						target = scope:liege
						modifier = thankful_consort_opinion
					}
				}
				# Effects for a secondary consort
				if = {
					limit = {
						scope:liege.primary_spouse ?= {
							NOT = {
								this = scope:employee
							}
						}
					}
					hidden_effect = {
						every_close_family_member = {
							limit = {
								NOR = {
									has_opinion_modifier = {
										target = scope:liege
										modifier = forced_family_concubine_marriage_opinion
									}
									this = scope:liege
									has_opinion_modifier = {
										target = scope:liege
										modifier = thankful_consort_relative_opinion
									}
								}
								hp_accepts_harems = yes
							}
							add_opinion = {
								target = scope:liege
								modifier = thankful_consort_relative_opinion
							}
						}
					}
				}
			}
		}

		# Effects for a secondary consort
		if = {
			limit = {
				trigger_if = {
					limit = {
						exists = scope:liege.primary_spouse
					}
					NOT = {
						this = scope:liege.primary_spouse
					}
				}
			}
			# Notify relatives
			every_close_family_member = {
				limit = { NOT = { this = scope:liege } }
				hidden_effect = {
					send_interface_message = {
						type = event_generic_good_text 
						title = grand_consort_promotion_t 
						right_icon = scope:employee
						left_icon = scope:liege
						desc = grand_consort_promotion_desc 
					}
				}
			}

			if = {
				limit = {
					NOT = {
						has_demoted_or_divorced_as_ps_opinion_trigger = {
							TARGET = scope:liege
						}
					}
				}
				# Clan unity
				add_clan_unity_interaction_effect = {
					CHARACTER = scope:liege
					TARGET = scope:employee
					VALUE = medium_unity_gain
					DESC = clan_unity_promotion.desc
					REVERSE_NON_HOUSE_TARGET = no
				}
				if = {
					limit = {
						NOT = {
							has_demoted_or_divorced_opinion_trigger = {
								TARGET = scope:liege
							}
						}
					}
					# Favor
					calyver_favor_huge_gain_effect = yes
					# Prestige
					if = {
						limit = { scope:liege.primary_title.tier = tier_kingdom }
						add_prestige = { value = minor_prestige_gain }
					}
					else_if = {
						limit = { scope:liege.primary_title.tier >= tier_empire }
						add_prestige = { value = medium_prestige_gain }
					}
				}
			}

			# Reinstatement
			if = {
				limit = {
					OR = {
						has_opinion_modifier = {
							modifier = demoted_me_as_gc
							target = scope:liege
						}
						has_opinion_modifier = {
							modifier = demoted_me_as_ss
							target = scope:liege
						}

						has_opinion_modifier = {
							modifier = divorced_me_as_gc
							target = scope:liege
						}
						has_opinion_modifier = {
							modifier = divorced_me_as_ss
							target = scope:liege
						}
					}

				}
				consort_reinstatement_effect = {
					REINSTATOR = scope:liege
					REINSTATED = scope:employee
				}
			}
		}
		else_if = { # Effects for the primary spouse
			limit = {
				scope:liege.primary_spouse ?= scope:employee
			}
			calyver_favor_medium_gain_effect = yes
		}
	}
}

#Basic effect for losing a court position
grand_consort_revoked_court_position_effect = {
	scope:employee = {
		if = {
			limit = { # Cannot lose more favor if they had been demoted before
				NOT = {
					has_demoted_or_divorced_as_ps_opinion_trigger = {
						TARGET = scope:liege
					}
				}
			}
			# Shared effects
			# No more favored modifiers
			clear_positive_consort_modifiers_effect = yes
			# No more positive opinion
			clear_consort_opinions_effect = {
				TARGET = scope:liege
			}
			# Harem members will be notified
			hidden_effect = {
				scope:liege = {
					every_courtier = {
						limit = {
							NOR = {
								this = scope:employee
							}
							is_valid_harem_member_trigger = {
								LIEGE = scope:liege
							}
						}
						send_interface_message = {
							type = event_generic_good_text
							title = punished_consort_t
							right_icon = scope:employee
							left_icon = scope:liege
							desc = demoted_grand_consort_interface_desc

							if = { #The primary spouse is relieved
								limit = {
									has_opinion_modifier = {
										target = scope:employee
										modifier = threatening_consort_opinion
									}
								}
								remove_opinion = {
									target = scope:employee
									modifier = threatening_consort_opinion
								}
							}
							if = { #Other consorts don't feel jealous
								limit = {
									has_opinion_modifier = {
										target = scope:employee
										modifier = envy_consort_opinion
									}
								}
								remove_opinion = {
									target = scope:employee
									modifier = envy_consort_opinion
								}
							}
							if = { #Craven consorts don't feel threatened by a despot
								limit = {
									scope:employee = {
										harem_politics_evil_consort_trigger = yes
									}
									is_consort_of = scope:liege
								}
								stress_impact = {
									craven = minor_stress_impact_loss
									shy = minor_stress_impact_loss
									paranoid = minor_stress_impact_loss
								}
							}
						}
					}
				}
			}
			#Progress towards rivals
			if = {
				limit = {
					can_set_relation_rival_trigger = { CHARACTER = scope:liege }
					NOT = {
						has_character_flag = block_rival_progress_effect_flag
					}
				}
				progress_towards_rival_effect = {
					REASON = demoted_me_reason
					CHARACTER = scope:liege
					OPINION = default_rival_opinion
				}
			}
			else_if = {
				limit = {
					has_character_flag = block_rival_progress_effect_flag
				}
				remove_character_flag = block_rival_progress_effect_flag
			}
			# Stress impact
			scope:liege = {
				stress_impact = {
					compassionate = minor_stress_impact_gain
					forgiving = minor_stress_impact_gain
				}
				if = {
					limit = { 
						AND = {
							has_trait = just
							NOT = { has_imprisonment_reason = scope:employee }
						}
					}
					stress_impact = {
						just = minor_stress_impact_gain
					}
				}
				if = {
					limit = { 
						has_trait = just
						has_imprisonment_reason = scope:employee
					}
					stress_impact = {
						just = minor_stress_impact_loss
					}
				}
			}

			# Effects for a secondary consort
			if = {
				limit = {
					trigger_if = {
						limit = {
							exists = scope:liege.primary_spouse
						}
						NOT = {
							this = scope:liege.primary_spouse
						}
					}
				}
				# If we're a clan this interaction affects unity
				add_clan_unity_interaction_effect = {
					CHARACTER = scope:liege
					TARGET = scope:employee
					VALUE = medium_unity_loss
					DESC = clan_unity_demotion.desc
					REVERSE_NON_HOUSE_TARGET = no
				}
				
				# Victim is naturally angry for being demoted.
				add_opinion = {
					target = scope:liege
					modifier = demoted_me_as_gc
				}
				# Favor impact
				if = {
					limit = {
						NOT = {
							has_character_flag = block_favor_progress_effect_flag
						}
					}	
					calyver_favor_big_loss_effect = yes
				}
				# Demoted modifier
				add_character_modifier = {
					modifier = demoted_spouse_modifier
				}
				if = {
					limit = {
						has_character_flag = block_favor_progress_effect_flag
					}
					remove_character_flag = block_favor_progress_effect_flag
				}

				#Relatives lose opinion and are notified
				every_close_family_member = {
					limit = { NOT = { this = scope:liege } } # don't hate yourself just because of demotion
					custom = all_close_family_members
					add_opinion = {
						modifier = demoted_concubine_marriage_opinion
						target = scope:liege
					}
					hidden_effect = {
						if = {
							limit = {
								has_opinion_modifier = {
									target = scope:liege
									modifier = thankful_consort_relative_opinion
								}
							}
							remove_opinion = {
								target = scope:liege
								modifier = thankful_consort_relative_opinion
							}
						}
						if = {
							limit = {
								has_opinion_modifier = {
									target = scope:liege
									modifier = reinstated_relative_marriage_opinion
								}
							}
							remove_opinion = {
								modifier = reinstated_relative_marriage_opinion
								target = scope:liege
							}
						}
						send_interface_message = {
							type = event_generic_bad_text
							title = demoted_grand_consort_t
							right_icon = scope:employee
							left_icon = scope:liege
							desc = demoted_grand_consort_interface_desc
						}
					}
				}
				# Prestige loss
				if = {
					limit = { scope:liege.primary_title.tier = tier_kingdom }
					add_prestige = { value = minor_prestige_loss }
				}
				else_if = {
					limit = { scope:liege.primary_title.tier >= tier_empire }
					add_prestige = { value = medium_prestige_loss }
				}
			}
			else_if = { # Effects for the primary spouse
				limit = {
					scope:liege.primary_spouse ?= scope:employee
				}
				# Favor loss
				calyver_favor_medium_loss_effect = yes
			}
		}
	}
}

#Invalidated position
grand_consort_invalidated_court_position_effect = {
}

#Grand Consort tasks effects#
grand_consort_improve_relationship_effect = {
	if = {
		limit = { 
			NOT = { this = $TARGET$ }
		}
		#Nemesis -> Rival
		if = {
			limit = {
				has_relation_nemesis = $TARGET$
			}
			remove_relation_nemesis = $TARGET$
			hidden_effect = {
				set_relation_rival = {
					target = $TARGET$
					reason = $REASON$
				}
			}
			add_opinion = {
				target = $TARGET$
				modifier = friendliness_opinion
				opinion = 15
			}
			reverse_add_opinion = {
				target = $TARGET$
				modifier = friendliness_opinion
				opinion = 15
			}
		}
		#Rival -> Potential Rival
		else_if = {
			limit = {
				has_relation_rival = $TARGET$
			}
			remove_relation_rival = $TARGET$
			hidden_effect = { set_relation_potential_rival = $TARGET$ }
			add_opinion = {
				target = $TARGET$
				modifier = friendliness_opinion
				opinion = 15
			}
			reverse_add_opinion = {
				target = $TARGET$
				modifier = friendliness_opinion
				opinion = 15
			}
		}
		#Potential Rival -> Potential Friend
		else_if = {
			limit = {
				has_relation_potential_rival = $TARGET$
			}
			remove_relation_potential_rival = $TARGET$
			hidden_effect = { set_relation_potential_friend = $TARGET$ }
			add_opinion = {
				target = $TARGET$
				modifier = friendliness_opinion
				opinion = 15
			}
			reverse_add_opinion = {
				target = $TARGET$
				modifier = friendliness_opinion
				opinion = 15
			}
		}
		#Potential Friend -> Friend
		else_if = {
			limit = {
				has_relation_potential_friend = $TARGET$
			}
			set_relation_friend = { 
				target = $TARGET$
				reason = $REASON$ 
			}
			add_opinion = {
				target = $TARGET$
				modifier = friendliness_opinion
				opinion = 30
			}
			reverse_add_opinion = {
				target = $TARGET$
				modifier = friendliness_opinion
				opinion = 30
			}
		}
		#Friend -> Best Friend
		else_if = {
			limit = {
				has_relation_friend = $TARGET$
				can_set_relation_best_friend_trigger = { CHARACTER = $TARGET$ }
			}
			set_relation_best_friend = { 
				target = $TARGET$
				reason = $REASON$  
			}
			add_opinion = {
				target = $TARGET$
				modifier = friendliness_opinion
				opinion = 50
			}
			reverse_add_opinion = {
				target = $TARGET$
				modifier = friendliness_opinion
				opinion = 50
			}
		}
		#Nothing -> Potential Friend
		else = {
			set_relation_potential_friend = $TARGET$
			add_opinion = {
				target = $TARGET$
				modifier = friendliness_opinion
				opinion = 15
			}
			reverse_add_opinion = {
				target = $TARGET$
				modifier = friendliness_opinion
				opinion = 15
			}
		}
	}
}

grand_consort_worsen_relationship_effect = {
	if = {
		limit = { 
			NOT = { this = $TARGET$ }
		}
		#Best Friend -> Friend
		if = {
			limit = {
				has_relation_best_friend = $TARGET$
			}
			remove_relation_best_friend = $TARGET$
			hidden_effect = { 
				set_relation_friend = { 
					target = $TARGET$
					reason = $REASON$
				} 
			}
			add_opinion = {
				target = $TARGET$
				modifier = unfriendly_opinion
				opinion = -15
			}
			reverse_add_opinion = {
				target = $TARGET$
				modifier = unfriendly_opinion
				opinion = -15
			}
		}
		#Friend -> Potential Friend
		else_if = {
			limit = {
				has_relation_friend = $TARGET$
			}
			remove_relation_friend = $TARGET$
			hidden_effect = { set_relation_potential_friend = $TARGET$ }
			add_opinion = {
				target = $TARGET$
				modifier = unfriendly_opinion
				opinion = -15
			}
			reverse_add_opinion = {
				target = $TARGET$
				modifier = unfriendly_opinion
				opinion = -15
			}
		}
		#Potential Friend -> Potential Rival
		else_if = {
			limit = {
				has_relation_potential_friend = $TARGET$
				NOT = { has_relation_potential_rival = $TARGET$ }
			}
			remove_relation_potential_friend = $TARGET$
			set_relation_potential_rival = $TARGET$
			add_opinion = {
				target = $TARGET$
				modifier = unfriendly_opinion
				opinion = -15
			}
			reverse_add_opinion = {
				target = $TARGET$
				modifier = unfriendly_opinion
				opinion = -15
			}
		}
		#Potential Rival -> Rival
		else_if = {
			limit = {
				has_relation_potential_rival = $TARGET$
			}
			set_relation_rival = {
				target = $TARGET$
				reason = $REASON$
			}
			add_opinion = {
				target = $TARGET$
				modifier = unfriendly_opinion
				opinion = -30
			}
			reverse_add_opinion = {
				target = $TARGET$
				modifier = unfriendly_opinion
				opinion = -30
			}
		}
		#Rival -> Nemesis
		else_if = {
			limit = {
				has_relation_rival = $TARGET$
				can_set_relation_nemesis_trigger = { CHARACTER = $TARGET$ }
			}
			set_relation_nemesis = {
				target = $TARGET$
				reason = $REASON$
			}
			add_opinion = {
				target = $TARGET$
				modifier = hate_opinion
				opinion = -50
			}
			reverse_add_opinion = {
				target = $TARGET$
				modifier = hate_opinion
				opinion = -50
			}
		}
		#Nothing -> Potential Rival
		else = {
			set_relation_potential_rival = $TARGET$
			add_opinion = {
				target = $TARGET$
				modifier = unfriendly_opinion
				opinion = -15
			}
			reverse_add_opinion = {
				target = $TARGET$
				modifier = unfriendly_opinion
				opinion = -15
			}
		}
	}
}